package com.wly
{
	import com.wly.data.ItemPool;
	import com.wly.data.ItemVO;
	import com.wly.data.ResourceCache;
	import com.wly.data.TypePool;
	import com.wly.data.TypeVO;
	import com.wly.dbc.XmlWriter;
	import com.wly.ui.AddType;
	import com.wly.ui.ItemDetailInfo;
	import com.wly.ui.ItemGrid;
	import com.wly.ui.MapEditer;
	import com.wly.ui.TypeGrid;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class ItemEditerMain extends Sprite
	{
		private var _typeGrid:TypeGrid;
		private var _itemDetailInfo:ItemDetailInfo;
		private var _btnSaveTypes:SimpleButton;
		private var _btnAddType:SimpleButton;
		private var _btnSaveItem:SimpleButton;
		private var _itemGrid:ItemGrid;
		private var _btnEditMap:SimpleButton;
		
		public function ItemEditerMain()
		{
			super();
			initChilds();
			init();
		}
		
		private function initChilds():void
		{
			_typeGrid       = getChildByName("mcTypeGrid") as TypeGrid;
			_typeGrid.itemEditMain = this;
			_itemDetailInfo = getChildByName("mcCurItemInfo") as ItemDetailInfo;
			_btnSaveTypes   = getChildByName("btnSaveTypes") as SimpleButton;
			_btnAddType     = getChildByName("btnAddType") as SimpleButton;
			_btnSaveItem    = getChildByName("btnSaveItem") as SimpleButton;
			_itemGrid       = getChildByName("mcItemGrid") as ItemGrid;
			_itemGrid.itemEditerMain = this;
			
			_btnEditMap     = getChildByName("btnEditMap") as SimpleButton;
			
			_btnSaveTypes.addEventListener(MouseEvent.CLICK, saveTypes);
			_btnAddType.addEventListener(MouseEvent.CLICK, addType);
			_btnSaveItem.addEventListener(MouseEvent.CLICK, saveItems);
			_btnEditMap.addEventListener(MouseEvent.CLICK, editMap);
		}
		
		private function init():void
		{
			ResourceCache.getInstance().init(onInitComplete);
		}
		
		private function onInitComplete():void
		{
			_typeGrid.setTypeItemList(TypePool.getInstance().getTypeList());
			_itemGrid.setItemList(ItemPool.getInstance().getItemList());
		}
		
		private function saveTypes(e:MouseEvent):void
		{
			XmlWriter.writeItemsTypes(ResourceCache.TYPEXMLFILE, TypePool.getInstance().getTypeList());
		}
		
		private function addType(e:MouseEvent):void
		{
			var addType:AddType = new AddType();
			addType.itemEditMain = this;
			addChild(addType);
		}
		
		private function saveItems(e:MouseEvent):void
		{
			XmlWriter.writeItemsList(ResourceCache.ITEMXMLFILE, ItemPool.getInstance().getItemList());
		}
		
		public function onTypesChange():void
		{
			_typeGrid.setTypeItemList(TypePool.getInstance().getTypeList());
		}
		
		public function onItemSelectChg(index:int):void
		{
			_itemDetailInfo.setItemInfo(_itemGrid.getItemVoByIndex(index));
			_typeGrid.setSelectedTypes(_itemGrid.getItemVoByIndex(index).types);
		}
		
		public function updateCurItemType(type:TypeVO, included:Boolean):void
		{
			var item:ItemVO = _itemDetailInfo.getItem();
			if(!item)
			{
				return;
			}
			
			if(included)
			{
				item.addType(type);
			}
			else
			{
				item.deType(type);
			}
		}
		
		private function editMap(e:MouseEvent):void
		{
			var mapEditer:MapEditer = new MapEditer;
			addChild(mapEditer);
		}
	}
}